/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.config;

import com.github.pagehelper.PageInterceptor;
import io.swagger.annotations.Api;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * <p>Project: ssm - SpringConfig
 * <p>Powered by webrx On 2021-11-24 16:26:11
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Configuration
@Import({DruidConfig.class})
@MapperScan(value = "cn.webrx.mapper",sqlSessionFactoryRef = "sf")
public class SpringConfig {
    /**
     * 注册SqlSessionFactory
     *
     * @param ds
     * @return
     */
    @Bean("sf")
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("ds") DataSource ds) {
        SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
        sf.setDataSource(ds);
        //mybatis 分页插件注册
        sf.setPlugins(pageInterceptor());
        sf.setTypeAliasesPackage("cn.webrx.entity");
        sf.setMapperLocations(resolveMapperLocations());
        return sf;
    }

    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath:mappers/*Mapper.xml");
        List<Resource> resources = new ArrayList<>();
        for (String mapperLocation : mapperLocations) {
            try {
                Resource[] mappers = resourceResolver.getResources(mapperLocation);
                resources.addAll(Arrays.asList(mappers));
            } catch (IOException ignored) {
            }
        }
        return resources.toArray(new Resource[0]);
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("cn.webrx.mapper");
        msc.setSqlSessionFactoryBeanName("sf");
        return msc;
    }

    /**
     * 配置分页插件(mybatis拦截器插件)
     *
     * @return
     */
    public PageInterceptor pageInterceptor() {
        var pi = new PageInterceptor();
        var prop = new Properties();
        prop.setProperty("reasonable", "true");
        pi.setProperties(prop);
        return pi;
    }




}
